#include "LoginWidget.h"
#include <QApplication>
#include <QStyle>
#include <QFont>
#include <QPalette>
#include <QGraphicsDropShadowEffect>
#include <QPropertyAnimation>
#include <QSequentialAnimationGroup>
#include <QPainter>
#include <QRect>
#include <QPoint>

LoginWidget::LoginWidget(QWidget *parent)
    : QWidget(parent)
{
    setupUI();
    setupConnections();
}

LoginWidget::~LoginWidget()
{
}

void LoginWidget::setupUI()
{
    // 主布局
    m_mainLayout = new QGridLayout(this);
    m_mainLayout->setContentsMargins(0, 254, 184, 0);
    m_mainLayout->setSpacing(0);
    
    // 创建登录框架
    m_loginFrame = new QFrame(this);
    m_loginFrame->setFixedSize(568, 420);
    m_loginFrame->setObjectName("loginFrame");
    
    // 框架布局
    m_frameLayout = new QVBoxLayout(m_loginFrame);
    m_frameLayout->setContentsMargins(64, 42, 64, 0);
    m_frameLayout->setSpacing(24);
    
    // 标题
    m_titleLabel = new QLabel("登录", m_loginFrame);
    m_titleLabel->setObjectName("titleLabel");
    m_titleLabel->setAlignment(Qt::AlignCenter);
    
    // 用户名输入区域
    m_usernameEdit = new QLineEdit(m_loginFrame);
    m_usernameEdit->setObjectName("usernameEdit");
    m_usernameEdit->setPlaceholderText("请输入账号");
    m_usernameEdit->setFixedSize(440, 48);
    
    m_usernameIcon = new QLabel(m_usernameEdit);
    m_usernameIcon->setStyleSheet("QLabel { background-color: transparent; }");
    m_usernameIcon->setGeometry(12, 12, 20, 20);
    m_usernameIcon->setPixmap(QPixmap(":/img/images/icon-user.png"));
    m_usernameIcon->setScaledContents(true);
    
    // 密码输入区域
    m_passwordEdit = new QLineEdit(m_loginFrame);
    m_passwordEdit->setObjectName("passwordEdit");
    m_passwordEdit->setPlaceholderText("请输入密码");
    m_passwordEdit->setEchoMode(QLineEdit::Password);
    m_passwordEdit->setFixedSize(440, 48);

    m_passwordIcon = new QLabel(m_passwordEdit);
    m_passwordIcon->setStyleSheet("QLabel { background-color: transparent; }");
    m_passwordIcon->setGeometry(12, 12, 20, 20);
    m_passwordIcon->setPixmap(QPixmap(":/img/images/icon-password.png"));
    m_passwordIcon->setScaledContents(true);
    
    // 选项区域
    m_optionsLayout = new QHBoxLayout();
    m_optionsLayout->setContentsMargins(0, 0, 0, 0);
    m_optionsLayout->setSpacing(0);
    
    m_rememberCheckBox = new QCheckBox("记住密码", m_loginFrame);
    m_rememberCheckBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    
    m_optionsLayout->addWidget(m_rememberCheckBox);
    m_optionsLayout->addStretch();
    
    // 登录按钮
    m_loginButton = new QPushButton("登录", m_loginFrame);
    m_loginButton->setObjectName("loginButton");
    m_loginButton->setFixedSize(440, 48);
    
    // 添加到框架布局
    m_frameLayout->addWidget(m_titleLabel, 0, Qt::AlignCenter);
    m_frameLayout->addWidget(m_usernameEdit, 0, Qt::AlignCenter);
    m_frameLayout->addWidget(m_passwordEdit, 0, Qt::AlignCenter);
    m_frameLayout->addLayout(m_optionsLayout);
    m_frameLayout->addWidget(m_loginButton, 0, Qt::AlignCenter);
    m_frameLayout->addStretch();
    
    m_mainLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum), 0, 0);
    m_mainLayout->addWidget(m_loginFrame, 0, 1, Qt::AlignCenter);
    m_mainLayout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding), 1, 1);
}

void LoginWidget::setupConnections()
{
    connect(m_loginButton, &QPushButton::clicked, this, &LoginWidget::onLoginClicked);
}

QString LoginWidget::getUsername() const
{
    return m_usernameEdit->text();
}

void LoginWidget::setUsername(const QString &username)
{
    m_usernameEdit->setText(username);
}

QString LoginWidget::getPassword() const
{
    return m_passwordEdit->text();
}

void LoginWidget::setPassword(const QString &password)
{
    m_passwordEdit->setText(password);
}

bool LoginWidget::isRememberPassword() const
{
    return m_rememberCheckBox->isChecked();
}

void LoginWidget::setRememberPassword(bool remember)
{
    m_rememberCheckBox->setChecked(remember);
}

void LoginWidget::clearForm()
{
    m_usernameEdit->clear();
    m_passwordEdit->clear();
    m_rememberCheckBox->setChecked(false);
}

void LoginWidget::onLoginClicked()
{
    QString username = getUsername();
    QString password = getPassword();
    
    if (username.isEmpty() || password.isEmpty()) {
        // 可以添加错误提示
        return;
    }
    
    emit loginRequested(username, password);
}

void LoginWidget::onForgotPasswordClicked()
{
    emit forgotPasswordClicked();
}
